package cn.whit.blog.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.StringUtils;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.whit.blog.domain.Article;


/**
 * solr工具类
 *
 */
public class SolrUtil {
	static final Logger logger = LoggerFactory.getLogger(SolrUtil.class);
	private static final String SOLR_URL = "http://192.168.52.120:8080/solr/collection1"; // 服务器地址
	private static HttpSolrServer server = null;
	static{
		try {
			server = new HttpSolrServer(SOLR_URL);
			server.setAllowCompression(true);
			server.setConnectionTimeout(10000);
			server.setDefaultMaxConnectionsPerHost(100);
			server.setMaxTotalConnections(100);
		} catch (Exception e) {
			logger.error("请检查tomcat服务器或端口是否开启!{}",e);
			e.printStackTrace();
		}
	}
	/**
	 * 建立索引
	 * @throws Exception
	 */
	public static void addIndex(Article article) {
		try {
			server.addBean(article);
			server.commit();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SolrServerException e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 查询
	 * @param skey 
	 * @param row 
	 * @param start 
	 * @param sort 
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public static Map<String,Object> search(String skey, Long start, Long row) throws Exception {
		SolrQuery solrQuery = new SolrQuery();
		solrQuery.set("q", "text:"+skey);
//		solrQuery.set("start", start+"");
//		solrQuery.set("rows", row+"");
		
		solrQuery.setHighlight(true);
		solrQuery.addHighlightField("title");
		solrQuery.addHighlightField("describe");
		solrQuery.setHighlightSimplePre("<font color='red'>");
		solrQuery.setHighlightSimplePost("</font>");
		
		
		QueryResponse response = server.query(solrQuery);
		List<Article> list = response.getBeans(Article.class);
		long numFound = response.getResults().getNumFound();
		System.out.println("numFount=" + numFound);
		System.out.println("list.size()" + list.size());
		Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
		for (Article article : list) {
			String id = article.getId();
			Map<String, List<String>> map = highlighting.get(id);
			List<String> titleList = map.get("title");
			if(titleList!=null && titleList.size()>0){
				article.setTitle(titleList.get(0));
			}
			
//			List<String> describelist = map.get("describe");
//			if(describelist!=null && describelist.size()>0){
//				article.setDesc(describelist.get(0));
//			}
		}
		
		
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("numFound", numFound);
		map.put("dataList", list);
		return map;
	}
	
	public static void main(String[] args) {
		System.out.println(server);
		
		Article article = new Article();
		article.setId(UUIDUtils.getCode());
		article.setTitle("安徽人口普查活动开始了");
		//在solr中建立该博客数据的索引数据
		SolrUtil.addIndex(article);
	}
	
	
	
}
